﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections;

namespace CMSLibrary.TagsCreate.CustomFunction.Inside
{
    /// <summary>
    /// 广告发布
    /// 1.  AD(广告编号)
    /// 2.  AD()
    /// </summary>
    public class Custom_AD : PInside
    {
        #region 参数成员
        /// <summary>
        /// 是否活动
        /// </summary>
        protected bool IsActive = true;
        /// <summary>
        /// 广告编号
        /// </summary>
        protected int ADID = 0;        
        #endregion

        #region 重写的方法
        protected override void SplitFormatParas(string sourceParas)
        {
            string[] paras = GetParasArray(sourceParas);
            //确定重载方法,由多到少            
            /********** 2 **********/
            try
            {
                ADID = Convert.ToInt32(paras[0]);
                IsActive = false;
                FunctionBurden = BurdenIndex.two;
                return;
            }
            catch { }            
        }

        public override string ToString()
        {
            return GetADToHTML(DataEntity.tab_Advertisement.SelectForShow(IsActive, ADID));
        }        
        #endregion

        #region 获取广告html内容
        public string GetADToHTML(DataTable dt)
        {
            DataEntity.tab_Advertisement ta = new DataEntity.tab_Advertisement();
            DataEntity.tab_AdZone tz = new DataEntity.tab_AdZone();

            StringBuilder sb = new StringBuilder();            

            if (dt != null && dt.Rows.Count != 0)
            {
                sb.Append("<!-- 广告 start -->")
                .Append("\r\n<script type='text/javascript'>");

                foreach (DataRow dr in dt.Rows)
                {
                    ta.SetValues(dr);
                    tz.SetValues(dr);

                    int mid = ta.adv_ID.Value;    //广告编号
                    string ZoneName = "ZoneName_" + mid.ToString();         //版位对象id
                    string ADObject = "ADObject_" + mid.ToString();         //广告对象id
                    string ZoneObject = SwitchZoneObject(tz.adz_ZoneType.Value);  //版位对象名称

                    
                    sb.Append("\r\nvar ").Append(ZoneName).Append(" = new ").Append(ZoneObject).Append("(\"").Append(ZoneName).Append("\");")
                        .Append("\r\n").Append(ZoneName).Append(".ZoneID      = ").Append(mid).Append(";")
                        .Append("\r\n").Append(ZoneName).Append(".ZoneWidth   = ").Append(tz.adz_ZoneWidth.Value).Append(";")
                        .Append("\r\n").Append(ZoneName).Append(".ZoneHeight  = ").Append(tz.adz_ZoneHeight.Value).Append(";")
                        .Append("\r\n").Append(ZoneName).Append(".ShowType  = ").Append(1).Append(";")
                        .Append("\r\n").Append(ZoneName).Append(".Left  = ").Append(tz.adz_ZoneLeft.Value).Append(";")
                        .Append("\r\n").Append(ZoneName).Append(".Top  = ").Append(tz.adz_ZoneTop.Value).Append(";")
                        .Append("\r\n\r\n")
                        .Append("var ").Append(ADObject).Append(" = new ObjectAD();")
                        .Append("\r\n").Append(ADObject).Append(".ADID           = ").Append(mid).Append(";")
                        .Append("\r\n").Append(ADObject).Append(".ADType         = ").Append(ta.adv_ADType.Value).Append(";")
                        .Append("\r\n").Append(ADObject).Append(".ADName         = ").Append("\"").Append(ta.adv_ADName).Append("\";")
                        .Append("\r\n").Append(ADObject).Append(".ImgUrl         = ").Append("\"").Append(ta.adv_ImgUrl).Append("\";")
                        .Append("\r\n").Append(ADObject).Append(".ImgWidth       = ").Append(ta.adv_ImgWidth.Value).Append(";")
                        .Append("\r\n").Append(ADObject).Append(".ImgHeight      = ").Append(ta.adv_ImgHeight.Value).Append(";")
                        .Append("\r\n").Append(ADObject).Append(".FlashWmode     = ").Append(Convert.ToInt32(ta.adv_FlashWmode.Value)).Append(";")
                        .Append("\r\n").Append(ADObject).Append(".ADIntro        = ").Append("\"").Append(ta.adv_Description).Append("\";")
                        .Append("\r\n").Append(ADObject).Append(".LinkUrl        = ").Append("\"").Append(ta.adv_LinkUrl).Append("\";")
                        .Append("\r\n").Append(ADObject).Append(".LinkTarget     = ").Append(Convert.ToInt32(ta.adv_LinkTarget.Value)).Append(";")
                        .Append("\r\n").Append(ADObject).Append(".LinkAlt        = ").Append("\"").Append(ta.adv_LinkAlt).Append("\";")
                        .Append("\r\n").Append(ADObject).Append(".Priority       = ").Append(1).Append(";")
                        .Append("\r\n").Append(ADObject).Append(".CountView      = ").Append(Convert.ToInt32(ta.adv_CountView.Value)).Append(";")
                        .Append("\r\n").Append(ADObject).Append(".CountClick     = ").Append(0).Append(";")
                        .Append("\r\n").Append(ADObject).Append(".InstallDir     = ").Append("\"").Append(UIMethod.ApplicationPath()).Append("\";")
                        .Append("\r\n").Append(ADObject).Append(".ADDIR          = ").Append("\"").Append(string.Empty).Append("\";")
                        .Append("\r\n").Append(ZoneName).Append(".AddAD(").Append(ADObject).Append(");")
                        .Append("\r\n").Append(ZoneName).Append(".Show();");
                }

                sb.Append("\r\n</script>")
                .Append("\r\n<!-- 广告 end -->");
            }

            return sb.ToString();
        }

        /// <summary>
        /// 返回版位对象名称
        /// </summary>
        /// <param name="ZoneType">类型编号</param>
        /// <returns></returns>
        string SwitchZoneObject(int ZoneType)
        {
            string result = "BannerZoneAD";
            switch (ZoneType)
            {
                case 1: //占位横幅
                    result = "BannerZoneAD";
                    break;
                case 2: //弹出窗口
                    result = "PopZoneAD";
                    break;
                case 3: //漂浮移动
                    result = "FloatZoneAD";
                    break;
                case 4: //固定位置
                    result = "FixedZoneAD";
                    break;
                case 5: //随屏移动
                    result = "MoveZoneAD";
                    break;
                case 6: //文字代码
                    result = "CodeZoneAD";
                    break;
            }
            return result;
        }
        #endregion
    }
}